package com.he.hsdyc.config.oss;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Component
public class OssClientUtils {

    @Autowired
    private OssConfig ossConfig;

    @Autowired
    private OssProperties ossProperties;


    /**
     * 上传图片
     * @param file
     * @param nfixName
     * @param schoolId
     * @return
     */
    public String uploadPic(MultipartFile file, String nfixName, Long schoolId, String module){


        String result="";  //返回图片路径
        OSSClient ossClient =null;
        String key=assembleOssPath(schoolId,module,nfixName);  //目标oss文件路径


        try {
            ossClient=ossConfig.getOSSClient();

            PutObjectResult tip=ossClient.putObject(ossProperties.getBucketName(), key, new ByteArrayInputStream(file.getBytes()));

            if(tip!=null && !"".equals(tip.getETag())) {
//                result = key;
            	result = ossProperties.getWebserver() + "/" + key;
            }


        }catch (Exception ex){
            ex.printStackTrace();
        }

        return result;



    }


    public String uploadPic(MultipartFile file, String nfixName, String module){


        String result="";  //返回图片路径
        OSSClient ossClient =null;
        String key=assembleOssPath(module,nfixName);  //目标oss文件路径


        try {
            ossClient=ossConfig.getOSSClient();

            PutObjectResult tip=ossClient.putObject(ossProperties.getBucketName(), key, new ByteArrayInputStream(file.getBytes()));

            if(tip!=null && !"".equals(tip.getETag())) {
//                result = key;
                result = ossProperties.getWebserver() + "/" + key;
            }


        }catch (Exception ex){
            ex.printStackTrace();
        }

        return result;



    }


    private List<MultipartFile> getFileList(HttpServletRequest request, String key){
        //创建一个通用的多部分解析器
        CommonsMultipartResolver multipartResovler = new CommonsMultipartResolver();
        //判断 request 是否有文件上传,即多部分请求
        if (!multipartResovler.isMultipart(request)) {
            //无附件上传
            return null;
        }
        // 转型为MultipartHttpRequest：
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        // 获得文件：
        List<MultipartFile> files = multipartRequest.getFiles(key);
        System.out.println(files.size());
        return files;
    }

    
    public String uploadImage(HttpServletRequest request, String module) {
        Map<String, MultipartFile> files = this.getFileMap(request);
        String result = "";
        if (files == null || files.keySet().size() <=0) {
            return result;
        }
        for(String key : files.keySet()){
            MultipartFile file = files.get(key);
            // 获取文件名
            String uploadFileName = file.getOriginalFilename();
            if(StringUtils.isBlank(uploadFileName)){
                return result;
            }
            //不带.的后缀
            String nfixName = uploadFileName.substring(uploadFileName.lastIndexOf(".")+1);
            result = this.uploadPic(file, nfixName, module);
            return result;
        }
        return null;
	}



    public Map<String, MultipartFile> getFileMap(HttpServletRequest request){
        //创建一个通用的多部分解析器
        CommonsMultipartResolver multipartResovler = new CommonsMultipartResolver();
        //判断 request 是否有文件上传,即多部分请求
        if (!multipartResovler.isMultipart(request)) {
            //无附件上传
            return null;
        }
        // 转型为MultipartHttpRequest：
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        // 获得文件：
        Map<String, MultipartFile> files = multipartRequest.getFileMap();
        return files;
    }

    //组装上传路径
    private String assembleOssPath(Long schoolId,String ossConstant,String suffix){

        return ossProperties.getKey()+schoolId+ossConstant+ UUID.randomUUID().toString().replace("-", "")+"."+suffix;
    }

    private String assembleOssPath(String ossConstant,String suffix){

        return ossProperties.getKey()+ossConstant+ UUID.randomUUID().toString().replace("-", "")+"."+suffix;
    }

}
